package com.example.lock.util;

import lombok.RequiredArgsConstructor;
import org.redisson.api.RAtomicLong;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

/**
 * <p>Description: </p>
 *
 * @author zg
 * @version v1.0.0
 * @since 2024/8/21
 **/
@RequiredArgsConstructor
@Component
public class IDGenerator {

    public static final String SEQ_STUDENT_ID = "seq:student_id";

    private final RedissonClient redissonClient;

    public Long generateSID() {
        RAtomicLong idCounter = redissonClient.getAtomicLong(SEQ_STUDENT_ID);
        long uniqueId = idCounter.incrementAndGet();
        return 10000 + uniqueId;
    }

    public void resetIDCounter(String seqName){
        RAtomicLong idCounter = redissonClient.getAtomicLong(seqName);
        idCounter.set(0);
    }

    public void resetIDCounter(String seqName, long newValue){
        RAtomicLong idCounter = redissonClient.getAtomicLong(seqName);
        idCounter.set(newValue);
    }
}
